$(function() {
	$('#form').validate({
		rules:{
			roleName: {
				required:true,
				maxlength: 45
			},
			roleType: {
				required:true
			},
			roleCode: {
				required:true,
				pattern: /^[0-9a-zA-Z]{4,45}$/,
				remote: {
					url: 'exist',
					type: 'get'
				}
			},
			remark: {
				maxlength: 255
			}
		},
		messages:{
			roleName:{
				required:'请输入名称',
				maxlength: '只允许输入45个字符'
			},
			roleType: {
				required: '请选择类型'
			},
			roleCode: {
				required: '请输入角色码',
				pattern: '在4至45个字符内',
				remote: '角色码已经存在'
				
			},
			remark: {
				maxlength:'最多输入255个字'
			}
		},
        submitHandler:function(form){
        	var json = $('#form').serializeJson();
        	if(validation.isEmpty(json.dictId)) {
        		save(json); 		
        	}else {
        		//update(json);
        	}
        	return false;
        }
	});
	
	function save(json) {
		$.loadReq({
			type:'post',
			url:'save',
			data:json,
			success: function(res) {
				if(res.status == 'true') {
					utils.palert('添加成功', function() {
						utils.closeLayer();
					});
				} else {
					utils.palert(res.msg);
				}
			}
		});
	}
	
	//修改
	function update(json) {
		$.loadReq({
			type:'post',
			url:'edit',
			data:json,
			success: function(res) {
				if(res.status == 'true') {
					utils.palert('修改成功', function() {
						utils.closeLayer();
					});
				} else {
					utils.palert(res.msg);
				}
			}
		});
	}
	
	$('.b-btn-save').click(function() {
		$('form').submit();
	});
});